package com.example.myapplication

import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 创建 ConstraintLayout 作为根布局
        val constraintLayout = ConstraintLayout(this)
        constraintLayout.id = R.id.main
        constraintLayout.layoutParams = ConstraintLayout.LayoutParams(
            ConstraintLayout.LayoutParams.MATCH_PARENT,
            ConstraintLayout.LayoutParams.MATCH_PARENT
        )

        // 创建 ImageView
        val imageView = ImageView(this)
        imageView.id = R.id.imageView
        imageView.setImageResource(R.drawable.flag)
        imageView.layoutParams = ConstraintLayout.LayoutParams(
            ConstraintLayout.LayoutParams.MATCH_PARENT,
            ConstraintLayout.LayoutParams.WRAP_CONTENT
        )

        // 创建 TextView
        val textView = TextView(this)
        textView.id = R.id.textView
        textView.textSize = 20f
        textView.text = getString(R.string.hello_world)
        textView.gravity = android.view.Gravity.CENTER

        // 创建 Button
        val button = Button(this)
        button.id = R.id.button
        button.text = getString(R.string.click)
        button.setOnClickListener {
            // 点击事件，更改按钮和 TextView 的文本
            button.text = getString(R.string.clicked)
            textView.text = getString(R.string.hello_kotlin)
        }

        // 将视图添加到 ConstraintLayout
        constraintLayout.addView(imageView)
        constraintLayout.addView(textView)
        constraintLayout.addView(button)

        // 使用 ConstraintSet 来设置约束
        val constraintSet = ConstraintSet()
        constraintSet.clone(constraintLayout)

        // 设置 ImageView 的约束
        constraintSet.connect(imageView.id, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP)
        constraintSet.connect(imageView.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
        constraintSet.connect(imageView.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)

        // 设置 TextView 的约束
        constraintSet.connect(textView.id, ConstraintSet.TOP, textView.id, ConstraintSet.BOTTOM, 16)
        constraintSet.connect(textView.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
        constraintSet.connect(textView.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)

//        // 设置 Button 的约束
        constraintSet.connect(button.id, ConstraintSet.TOP, textView.id, ConstraintSet.BOTTOM, 1700)
        constraintSet.connect(button.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
        constraintSet.connect(button.id, ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END)

        // 应用 ConstraintSet
        constraintSet.applyTo(constraintLayout)

        // 设置根布局
        setContentView(constraintLayout)
    }
}